/*****************************************************************************************
**  Author:dgz master
**  Contact：jiduxiong@dagongzao.com
**
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码

let extension = {
  components: {
    //查询界面扩展组件
    gridHeader: "",
    gridBody: "",
    gridFooter: "",
    //新建、编辑弹出框扩展组件
    modelHeader: "",
    modelBody: "",
    modelRight: "",
    modelFooter: ""
  },
  tableAction: "", //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  device_state:{"0":"禁用","10":"待上料","11":"上磨料中","12":"上工件中","13":"上料完成",
                "20":"待研磨","21":"研磨中","22":"研磨完成","30":"待下料","31":"下料中","32":"下料完成",},
  methods: {
    
     //下面这些方法可以保留也可以删除
    onInit() {  //框架初始化配置前，
        //示例：在按钮的最前面添加一个按钮
        //   this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
        //     name: "按钮", //按钮名称
        //     icon: "el-icon-document", //按钮图标：https://element.eleme.cn/#/zh-CN/component/icon
        //     type: "primary", //按钮样式:https://element-plus.gitee.io/zh-CN/component/button.html
        //     //color:"#eee",//自定义按钮颜色
        //     onClick: function () {
        //       this.$Message.success("点击了按钮");
        //     }
        //   });

        //示例：设置修改新建、编辑弹出框字段标签的长度
        // this.boxOptions.labelWidth = 150;
        this.pagination = {
        size: 100,//默认分页大小写
        // sizes: [10, 20, 30, 60],
        sortName: "little_lot",
        order: "asc"//排序方式
      }  
      
    },
    onInited() {
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
      this.initFormSelectTable();
      this.initFormSelectTable_tech();
      for(let i=0;i<this.columns.length;i++){
        this.columns[i]["align"] = "center"
      }
      // this.buttons = []
      //  this.buttons.splice(2,0,{
      //     name: "分配设备",
      //     icon: "el-icon-remove",
      //     type: "warning",
      //     onClick: () =>{
      //       // this.complete(anniu_pro);
      //       this.manual_updata_data()
      //     }
      //   })
      for(let i=0;i<this.buttons.length;i++){
        if(this.buttons[i].name == "编辑"){
          this.buttons[i].name = "分配设备"
          this.buttons[i].type = "primary"
          this.buttons[i].plain = false
          this.buttons[i].icon = "el-icon-remove"
        }
        console.log(this.buttons)
      }
      this.buttons.splice(4, 0, {
        name: "刷新",
        icon: "el-icon-remove",
        type: "warning",
        onClick: () => {
          // this.complete(anniu_pro);
          this.updata_order_state()
        }
      })
    },
    updata_order_state(){
      console.log("G11111111111111")
      console.log(this.$store.getters.data().name)
      let a = this.$store.getters.data().name
      let order_name = []
      order_name.push(a)
      let jsonString_mes_data = JSON.stringify(order_name)
      this.http.get('api/daojiaoGD/get_per_little_lot_state',
          { order_name: a }).then((reslut) => {
            console.log(reslut[0])
            this.search()
          }).catch((error) => {
            console.log("失败", error)
            return "未更新"
          });
    },
    initFormSelectTable(item) {
      var ccdstatus = {"run":"运行","fault":"故障","waiting":"待机","no":"未排产","other":"其他"}
      console.log("KKKKKKKK")
      this.editFormOptions.forEach((option) => {
        option.forEach((item) => {
          if (item.field == 'device') {
            //if（this.editFormFields.DianduNo.）
            //电镀号处理下拉框
            console.log("KKK")
            item.url = 'api/daojiaoGD/shebei_db'  //后端代码中<ATkg.BizAppProject/Controllers/AmesSyncController.cs中的Route
            //item.url = 'api/AmesSyncBiz/SyncMaterialInventory'  //后端代码中<ATkg.BizAppProject/Controllers/AmesSyncController.cs中的Route
            item.columns = [
              { field: 'name', title: '设备名称', type: 'string', width: 70, hidden: false,align:"center" },
              { field: 'ccd_status', title: '设备状态', type: 'string', width: 50, hidden: false,align:"center"  },
              { field: 'xianti_numb', title: '所属线体', type: 'string', width: 50, hidden: false,align:"center"  },
            ]
            item.onSelect = (rows) => {
              //给表单字段赋值
              this.editFormFields.device = rows[0].name
              this.editFormFields.ccd_status = ccdstatus[rows[0].ccd_status]
            }
            item.loadBefore = (param, callback) => {
              // param.value = this.editFormFields.DianduNo;
              callback(true);
            }
            item.loadAfter = (rows, callback, result) => {
              callback(true);
            }
            //监听输入框变动与回车事件
            item.onKeyPress = (val, $event) => {
              // console.log(val);
              // if ($event.keyCode == 13) {
              //   console.log('按了回车');
              // }
            }
            //设置弹出框高度(默认200)
            item.height = 300;
            //设置弹出框宽度(默认500)
            item.width = 300;
            item.textInline = true; //设置表格超出自动换行显示
            //设置表格是否单选
            item.single = true;
            //隐藏分页
            item.paginationHide = true;
          }
        })
      })
    },
    initFormSelectTable_tech(item) {
      console.log("23423423")
      this.editFormOptions.forEach((option) => {
        option.forEach((item) => {
          if (item.field == 'tech') {
            console.log("23444234444")            
            item.url = 'api/daojiaoGD/get_tech_db_lot_chaif'  //后端代码中<ATkg.BizAppProject/Controllers/AmesSyncController.cs中的Route
            console.log("LL",item)
            item.columns = [
              { field: 'Name', title: '工艺名称', type: 'string', width: 100, hidden: false,align:"center" },
              { field: 'ProductName', title: '产品代码', type: 'string', width: 80, hidden: false,align:"center"  },
              { field: 'GuiGe', title: '规格', type: 'string', width: 80, hidden: false,align:"center"  },
              { field: 'XinNneng', title: '性能', type: 'string', width: 80, hidden: false,align:"center"  },
            ]
            item.onSelect = (rows) => {
              //给表单字段赋值
              this.editFormFields.tech = rows[0].Name
            }
            item.loadBefore = (param, callback) => {
              // param.value = this.editFormFields.DianduNo;
              callback(true);
            }
            item.loadAfter = (rows, callback, result) => {
              callback(true);
            }
            //监听输入框变动与回车事件
            item.onKeyPress = (val, $event) => {
              // console.log(val);
              // if ($event.keyCode == 13) {
              //   console.log('按了回车');
              // }
            }
            //设置弹出框高度(默认200)
            item.height = 300;
            //设置弹出框宽度(默认500)
            item.width = 500;
            item.textInline = true; //设置表格超出自动换行显示
            //设置表格是否单选
            item.single = true;
            //隐藏分页
            item.paginationHide = true;
          }
        })
      })
    },
    manual_updata_data(){
      console.log("工单跟踪")
    },
    searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      console.log("222222222",this.$store.getters.data().name)
      console.log("param",param)
      // param.wheres.push({
      //     name: 'name',
      //     value: this.$store.getters.data().name,
      //   })
      // console.log("会来这里吗？",param)
      // return true;
      if (this.$store.getters.data().name) {
        param.wheres.push({
          name: 'name',
          value: this.$store.getters.data().name,
        })
        console.log("??????",param)
      } else {
        this.$message.error('请先选择工单')
        return false
      }
      
      return true
    },
    getKeyByValue(obj, targetValue) {
      return Object.keys(obj).find(key => obj[key] === targetValue);
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      console.log("这里呢？",result,this.$store.getters.data().name)
      var device_state = {"0":"禁用","10":"待上料","11":"上磨料中","12":"上工件中","13":"上料完成",
                "20":"待研磨","21":"研磨中","22":"研磨完成","30":"待下料","31":"下料中","32":"下料完成"}

      this.columns.find((x) => {   
            return x.field == 'jingdu'
          }).render = (h, { row, column, index }) => {
            const currentStep1 = row.current_state; // 假设row中有currentStep字段记录当前进度
            let currentStep = "0"
            let sta_num = this.getKeyByValue(device_state, currentStep1)
            if(currentStep1 != null){
              if(sta_num < 20){
                currentStep = "1"
              }else if(20 <= sta_num < 30){
                currentStep = "2"
              }else if(30 <= sta_num){
                currentStep = "3"
              }
            }       
            return (
              <div>
                <el-steps active={currentStep} align-center>
                  <el-step title="上料" ></el-step>
                  <el-step title="研磨" ></el-step>
                  <el-step title="下料" ></el-step>
                </el-steps>
              </div>
            )
          }
      
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值

      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    delBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
   addAfter(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      //添加后刷新字典

      return true;
    },
    updateAfter(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      //添加后刷新字典
      console.log("??1231231231231231")
      console.log(formData)
      let insert_data = {
          name:formData.data.device,
          current_order:formData.data.little_lot
        }

        
      let jsonString_mes_data = JSON.stringify(insert_data)
      console.log(jsonString_mes_data)
      this.http.get('/api/daojiaoGD/insert_dvices_current_order',
                  {mes_data:jsonString_mes_data}).then((reslut) => {
                }).catch((error) => {
                    console.log("失败",error)
                });
      return true;
    },
    delAfter(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      //添加后刷新字典
      
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      // this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=="Add";
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段="xxx";
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
    }
  }
};
export default extension;
